package com.example.myapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private EditText userId=null;
    private EditText userPassword=null;
    private Button confirmButton=null;
    private Button cancelButton=null;
    private RadioGroup radioGroup=null;
    private String sex=null;
    private CheckBox cb1,cb2,cb3;
    private String likes=null;
    private TextView textChange=null;


    //onCreate相当于主界面的构造函数,程序运行时自动调用
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userId=findViewById(R.id.user_id);
        userPassword=findViewById(R.id.user_password);
        confirmButton=findViewById(R.id.confirm_button);
        cancelButton=findViewById(R.id.cancel_button);
        radioGroup=findViewById(R.id.sex_radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==R.id.man)
                {
                    sex="男";
                }
                else if(i==R.id.woman)
                {
                    sex="女";
                }
            }
        });

        cb1=findViewById(R.id.cb_game);
        cb2=findViewById(R.id.cb_sport);
        cb3=findViewById(R.id.cb_read);

        //写一个监听器
        CompoundButton.OnCheckedChangeListener cbListener=new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                {
                    if(likes==null)
                    {
                        likes=compoundButton.getText().toString()+" ";
                    }
                    else
                    {
                        likes+=compoundButton.getText().toString()+" ";
                    }
                }
            }
        };
        cb1.setOnCheckedChangeListener(cbListener);
        cb2.setOnCheckedChangeListener(cbListener);
        cb3.setOnCheckedChangeListener(cbListener);

        textChange=findViewById(R.id.text_size);
    }

    public void confirm_click(View view) {
        String msg="用户名:"+userId.getText()+"  "+"密码:"+userPassword.getText()+"  "+"性别"+sex+" "+"喜欢"+likes;
        Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
    }

    public void cancel_click(View view) {

        //创建普通对话框
        AlertDialog.Builder myDialogBuilder=new AlertDialog.Builder(this);
        myDialogBuilder.setIcon(R.mipmap.ic_launcher);
        myDialogBuilder.setTitle("对话框");
        myDialogBuilder.setMessage("确认要退出吗");
        myDialogBuilder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                MainActivity.this.finish();
            }
        });
        myDialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        AlertDialog myDialog=myDialogBuilder.create();
        myDialog.show();
    }
    int index = 0;
    public void size_click(View view) {
        AlertDialog.Builder myBuilder=new AlertDialog.Builder(this);
        myBuilder.setTitle("调整字体大小");
        myBuilder.setIcon(R.mipmap.ic_launcher);
        int[] textSize=new int[]{10,20,30,40,50};
        myBuilder.setSingleChoiceItems(new String[]{"小号", "中号", "大号"}, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //i是被选项的序号
                index=i;
            }
        });
        myBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                textChange.setTextSize(textSize[index]);
                dialogInterface.dismiss();
            }
        });
        myBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });

        AlertDialog myDialog=myBuilder.create();
        myDialog.show();
    }

    public void multi_click(View view) {
        String[] str=new String[]{"吃饭","睡觉","旅游"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        boolean[] choice=new boolean[]{false,false,false};

        builder.setMultiChoiceItems(str, choice, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                //i是选中项序号，b是被点击项是否处于选中状态
                choice[i]=b;
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                StringBuffer stringBuffer=new StringBuffer();
                for(int j=0;j<choice.length;j++)
                {
                    if(choice[j])
                    {
                        stringBuffer.append(str[j]);
                        stringBuffer.append(" ");
                    }
                }
                Toast.makeText(MainActivity.this,stringBuffer,Toast.LENGTH_SHORT).show();
                dialogInterface.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });

        AlertDialog myDialog=builder.create();
        myDialog.show();
    }

//    public void confirm_click(View view)
//    {
//        String msg="用户名:"+userId.getText()+"  "+"密码:"+userPassword.getText();
//        Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
//    }
}